package com.cetccst.securitysuite.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.cetccst.securitysuite.network.LoginHttpRequester;

import java.lang.ref.WeakReference;

/**
 *
 */
@SuppressLint("MissingPermission")
public class NetworkUtils {

    public static final String CA_ROOT_CERT_NAME = "private_boot.pem";

    private static NetworkUtils instance;
    private WeakReference<Context> contextWeakReference;

    private NetworkUtils(Context context) {
        contextWeakReference = new WeakReference<>(context);
    }

    public static void init(Context context) {
        instance = new NetworkUtils(context);
    }

    public static NetworkUtils getInstance() {
        return instance;
    }

    public boolean isNetworkConnected() {
        assert contextWeakReference != null;
        ConnectivityManager manager = (ConnectivityManager) contextWeakReference.get().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo;
        if (manager != null) {
            networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                return true;
            }
        }
        return false;
    }

    public boolean isWifiEnabled() {
        assert contextWeakReference != null;
        WifiManager wifiManager = (WifiManager)
                contextWeakReference.get().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return wifiManager != null && wifiManager.isWifiEnabled();
    }

    public boolean isWIFIConnected() {
        assert contextWeakReference != null;
        ConnectivityManager manager = (ConnectivityManager)
                contextWeakReference.get().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info;
        if (manager != null) {
            info = manager.getActiveNetworkInfo();
            return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI;
        }
        return false;
    }

    public boolean isMobileDataConnected() {
        assert contextWeakReference != null;
        ConnectivityManager manager = (ConnectivityManager)
                contextWeakReference.get().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info;
        if (manager != null) {
            info = manager.getActiveNetworkInfo();
            return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE;
        }
        return false;
    }

    @SuppressLint("HardwareIds")
    public String getMacAddress() {
        assert contextWeakReference != null;
        WifiManager wifi = (WifiManager)
                contextWeakReference.get().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info;
        if (wifi != null) {
            info = wifi.getConnectionInfo();
            if (info != null) {
                return info.getMacAddress();
            }
        }
        return "";
    }

    public String getRunningAPNProfile() {
        assert contextWeakReference != null;
        ConnectivityManager manager = (ConnectivityManager)
                contextWeakReference.get().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo;
        if (manager != null) {
            networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                return networkInfo.getExtraInfo();
            }
        }
        return "";
    }

    public String getErrorPage() {
        return LoginHttpRequester.getInstance().getErrorPage();
    }
}
